package ventottoGiuDuemilaundici;

import java.util.LinkedList;

public class RicercaHeap {
	public static void main(String[] args) {
		HeapBinaryTree<Integer> albero = new HeapBinaryTree<Integer>();
		albero.addRoot(1, 1);
		HNode<Integer> cur = albero.root();
		albero.insertLeft(cur, 2, 2);
		albero.insertRight(cur, 3, 3);
		cur = cur.getLeft();
		albero.insertLeft(cur, 4, 4);
		albero.insertRight(cur, 5, 5);
		cur = albero.root();
		cur = cur.getRight();
		albero.insertLeft(cur, 6, 6);
		albero.insertRight(cur, 7, 7);
		LinkedList<Integer> ris = trovaChiave(albero, 10);
		while(!ris.isEmpty()) {
			int temp = ris.removeFirst();
			System.out.print(temp+" ");
		}
		
	}
	public static <E> LinkedList<Integer> trovaChiave(HeapBinaryTree<E> tree, int T) {
		LinkedList<Integer> chiavi = new LinkedList<Integer>();
		HNode<E> cur = tree.root();
		ricorsivo(tree, T, cur, chiavi);
		return chiavi;
	}
	public static <E> void ricorsivo(HeapBinaryTree<E> tree, int T, HNode<E> cur, LinkedList<Integer> chiavi) {
		if(cur.getKey()<=T) {
			chiavi.addLast(cur.getKey());
			if(cur.hasLeft())
				ricorsivo(tree, T, cur.getLeft(), chiavi);
			if(cur.hasRight())
				ricorsivo(tree, T, cur.getRight(), chiavi);
		}
	}

}
